package com.yf.gogameserver.service;/*
@author FAN

@create 2021-12-21-16:42


*/

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yf.gogameserver.dao.UserDao;
import com.yf.gogameserver.entity.User;
import com.yf.gogameserver.utils.MapCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Date;

@Service
@Primary
public class UserDBServiceImpl implements IUserService {
    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private UserDao userDao;

    //用户注册
    @Override
    public int register(User user) {
        //先验证用户是否存在
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", user.getUsername());
        int count = userDao.selectCount(queryWrapper);
        if (count > 0) {
            //用户名存在
            return -1;
        }
        //验证邮箱是否存在
        QueryWrapper queryWrapper2 = new QueryWrapper();
        queryWrapper2.eq("email", user.getEmail());
        int ecount = userDao.selectCount(queryWrapper2);
        if (count > 0) {
            //用户名存在
            return -2;
        }


        return userDao.insert(user);
    }


    //用户登录
    @Override
    public User login(String username, String password) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", username);
        queryWrapper.eq("password", password);
        User user = userDao.selectOne(queryWrapper);
        return user;
    }

    /**
     * 通过账号发送邮件，通过邮箱得到新密码
     *
     * @param username
     * @return
     */
    @Override
    public int sendEmailByUserName(String username) {
        //判断用户名是否存在
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", username);
        int count = userDao.selectCount(queryWrapper);
        if (count == 0) {
            //用户名存在
            return -1;
        }
        //通过用户名查询邮箱
        QueryWrapper queryWrapper2 = new QueryWrapper();
        queryWrapper2.eq("username", username);
        User user = userDao.selectOne(queryWrapper2);
        //获取当前用户邮箱
        String email = user.getEmail();
        //生成验证码 4,转型int，舍去小数部分
        int code = (int) (Math.random() * 9000 + 1000);
        //map集合：username，（code，time）
        MapCodeUtils.putCode(username, code + "");


        //给当前邮箱发送邮件  mimeMessage邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //将邮件对象修饰，方便设置
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage);
        //设置邮件相关属性
        try {
            //邮件标题
            mimeMessageHelper.setSubject("yf网络找回邮件");
            //邮件发送方
            mimeMessageHelper.setFrom("yf199712@sina.com");
            //收件方
            mimeMessageHelper.setTo(email);//普通接收方
//            mimeMessageHelper.setCc();//发送抄送方
//            mimeMessageHelper.setBcc();//发送密送方
            //邮件内容
            mimeMessageHelper.setText("<h1>找回密码</h1><img src='https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2Ftp09%2F21042G4331941H-0-lp.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1642732079&t=231f8d46f350042516d7cdc5d0b583d0'>找回密码的验证码:" + code + "如果非本人操作请忽略", true);
            //发送时间
            mimeMessageHelper.setSentDate(new Date());
            //添加附件
//            mimeMessageHelper.addAttachment("我的附件",new File("D:\\imag\\2.jpg"));


            //发送邮件
            javaMailSender.send(mimeMessage);
            return 1;//发送成功
        } catch (MessagingException e) {
            e.printStackTrace();
        }

        return -2;//邮件发送失败
    }

    /**
     * 根据用户名重置密码
     *
     * @param username
     * @param newPassword
     * @return
     */
    @Override
    public int updatePassword(String username, String newPassword) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", username);
        User user = userDao.selectOne(queryWrapper);
        //重新设置密码
        user.setPassword(newPassword);
        //修改
        int result = userDao.updateById(user);

        return result;
    }

    /**
     * 根据用户名修改头像
     *
     * @param username
     * @param header
     * @return
     */
    @Override
    public int updateHeader(String username, String header) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", username);
        User user = userDao.selectOne(queryWrapper);
        //重新设置用户头像
        user.setHeader(header);

        int result = userDao.updateById(user);

        return result;
    }
}
